package com.fr.chicken.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {
        /**
         * 时间戳转换成日期格式字符串
         * @param seconds 精确到秒的字符串
         * @param format 如：yyyy-MM-dd HH:mm:ss
         * @return
         */
        public static String timeStampDateString(String seconds,String format) {
            if(seconds == null || seconds.isEmpty() || seconds.equals("null")){
                return "";
            }
            if(format == null || format.isEmpty()){
                format = "yyyy-MM-dd HH:mm:ss";
            }
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            return sdf.format(new Date(Long.valueOf(seconds+"000")));
        }
        /**
         * 时间戳转换成日期格式字符串
         * @param seconds 精确到秒的字符串
         * @param format 如：yyyy-MM-dd HH:mm:ss
         * @return
         */
        public static Date timeStampDate(String seconds,String format) {
            if(seconds == null || seconds.isEmpty() || seconds.equals("null")){
                return null;
            }
            if(format == null || format.isEmpty()){
                format = "yyyy-MM-dd HH:mm:ss";
            }
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            try {
                return sdf.parse(seconds);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            return null;
        }

        /**
         * 日期格式字符串转换成时间戳
         * @param date_str 字符串日期
         * @param format 如：yyyy-MM-dd HH:mm:ss
         * @return
         */
        public static String dateTimeStamp(String date_str,String format){
            try {
                SimpleDateFormat sdf = new SimpleDateFormat(format);
                return String.valueOf(sdf.parse(date_str).getTime()/1000);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return "";
        }

        /**
         * 取得当前时间戳（精确到秒）
         * @return
         */
        public static String timeStamp(){
            long time = System.currentTimeMillis();
            String t = String.valueOf(time/1000);
            return t;
        }

        /**
         * 获取今日的日期
         * */
        public static String getCurrentTime(){
            //使用Calendar
//            Calendar now = Calendar.getInstance();
//            System.out.println("年：" + now.get(Calendar.YEAR));
//            System.out.println("月：" + (now.get(Calendar.MONTH) + 1));
//            System.out.println("日：" + now.get(Calendar.DAY_OF_MONTH));
//            System.out.println("时：" + now.get(Calendar.HOUR_OF_DAY));
//            System.out.println("分：" + now.get(Calendar.MINUTE));
//            System.out.println("秒：" + now.get(Calendar.SECOND));
            //使用Date
            Date d = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            return sdf.format(d);
        }

}
